package com.hncic.tx.boot;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

import com.hncic.tx.center.common.util.UtilJson;
import com.hncic.tx.center.mvc.util.HttpAuthorizeFilter;

/**
 * @author smj E-mail: 463136320@qq.com
 * @created 2017年8月4日 下午6:27:06
 * @since jdk1.8
 * @version 1.0
 * @describe
 */
@SpringBootApplication
@ComponentScan("com.hncic.tx.center")
public class Application {  
	private static final String config = "app.properties";
	private static Logger logger = LoggerFactory.getLogger(Application.class);

	public static void main(String[] args) {
		Resource resource = new ClassPathResource(config);
		EncodedResource encRes = new EncodedResource(resource, "UTF-8");
		Properties appProperties = null;
		try {
			appProperties = PropertiesLoaderUtils.loadProperties(encRes);
		} catch (Exception e) {
			logger.error("Application 启动失败!!!,配置文件:{}读取错误,error!!", config, e);
			throw new RuntimeException(e);
		}

		SpringApplication app = new SpringApplication(Application.class);
		app.setDefaultProperties(appProperties);
		app.setBannerMode(Banner.Mode.OFF);
		app.run(args);
	}

	/*
	 * 过滤器fitler
	 */
	@Bean
	public FilterRegistrationBean filterRegistrationBean() {
		FilterRegistrationBean registrationBean = new FilterRegistrationBean();
		List<String> urlPatterns = new ArrayList<String>();


		HttpAuthorizeFilter httpBasicFilter = new HttpAuthorizeFilter();
		registrationBean.setFilter(httpBasicFilter);

		urlPatterns.add("/api/admin/*");
		urlPatterns.add("/api/car/*");

		registrationBean.setUrlPatterns(urlPatterns);
		return registrationBean;
	}

	/*
	 * json格式
	 */
	@Bean
	public MappingJackson2HttpMessageConverter jackson2HttpMessageConverter() {
		MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(UtilJson.mapper0);
		return converter;
	}

}  